<?php

namespace App\Http\Controllers\SystemManagement;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Constant\Fields;
use App\Models\SystemManagement\Skill;
use App\Models\SystemManagement\PostSkill;
use App\Models\SystemManagement\Employee;
use App\Models\Constant\Errors;
use App\Models\Error;
use Illuminate\Support\Facades\DB;

/**
 * @group 职级管理
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */

class SkillController extends Controller
{
    /**
     * 职级查看列表
     *
     * @queryParam  name string 职级名称
     * @queryParam  organization_id int 所属组织ID
     * @queryParam  department_id int 所属部门ID
     * @queryParam  post_id int 岗位等级ID
     * @queryParam  remark string 备注
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $skill = new Skill;
        $data = $skill->getList($request);
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 新增职级
     *
     * @bodyParam  name string required 职级名称
     * @bodyParam  remark string optional 备注
     * @bodyParam  post_id int required 岗位ID
     *
     * @queryParam  name string 职级名称
     * @queryParam  remark string 备注
     * @queryParam  post_id int 岗位ID
     *
     * @return \Illuminate\Http\Response
     */
    public function store(\App\Http\Requests\System\Skill $request)
    {
        DB::beginTransaction();
        try {
            $data = Skill::create($request->all());
            $skill['post_id'] = $request->input('post_id','');
            $skill['skill_id'] = $data->id;
            try{
                PostSkill::create($skill);
            }catch (\Exception $e){
                DB::rollBack();
                return response()->json(encodeMsg(["message" => "职级信息添加失败".$e->getMessage()],404), 404);
            }
            DB::commit();
            return response()->json(encodeMsg($data,200));
        } catch (QueryException $e) {
            DB::rollBack();
            return response()->json(encodeMsg(['message' => '职级信息添加失败!'],400));
        }
    }

    /**
     * 查看职级
     *
     * @bodyParam  id int required 职级ID
     *
     * @queryParam  name string 职级名称
     * @queryParam  remark string 备注
     * @queryParam  post_id int 岗位ID
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = Skill::find($id);
        if($data == []) {
            return response()->json(encodeMsg(["message" => "没有该职级信息!"], 404));
        } else {
            return response()->json(encodeMsg($data,200));
        }
    }

    /**
     * 修改职级
     *
     * @bodyParam  name string required 职级名称
     * @bodyParam  remark string optional 备注
     * @bodyParam  post_id int required 岗位ID
     *
     * @queryParam  name string 职级名称
     * @queryParam  remark string 备注
     * @queryParam  post_id int 岗位ID
     *
     * @return \Illuminate\Http\Response
     */
    public function update(\App\Http\Requests\System\Skill $request, $id)
    {
        $skill = Skill::find($id);
        if($skill == []) {
            return response()->json(encodeMsg(["message" => "没有该职级信息!"], 404));
        }
        DB::beginTransaction();
        try {
            $data['post_id'] = $request->input('post_id','');
            $data['skill_id'] = $id;
            try{
                PostSkill::where('skill_id',$id)->delete();
                PostSkill::create($data);
            }catch (\Exception $e){
                DB::rollBack();
                return response()->json(encodeMsg(["message" => "职级信息修改失败".$e->getMessage()],404), 404);
            }
            $skill->update($request->all());
            DB::commit();
            return response()->json(encodeMsg($skill,200));
        } catch (QueryException $e) {
            DB::rollBack();
            // dd($e);
            return response()->json(encodeMsg(['message' => '职级信息修改失败!'],400));
        }
    }

    /**
     * 删除职级
     *
     * @bodyParam id int required  职级ID
     * @response 204 {
     *   "message": "职级信息删除成功"
     * }
     *
     *
     * @response 404 {
     *  "message": "没有该职级信息"
     * }
     */
    public function destroy($id)
    {
        $skill = Skill::find($id);
        if($skill == []) {
            return response()->json(encodeMsg(["message" => "没有该职级信息!"], 404));
        }
        if($skill->delete()) {
            return response()->json(encodeMsg(["message" => "职级信息删除成功!"], 204));
        } else {
            return response()->json(encodeMsg(["message" => "职级信息删除失败!"], 400));
        }
    }

    /**
     * 批量删除职级
     *
     * @param  int  $ids id集合字符串 如:1,2,3,4
     * @return \Illuminate\Http\Response
     */
    public function delete($ids)
    {
        $listId = explode(',', $ids);
        Skill::whereIn('id',$listId)->delete();
        return response()->json(encodeMsg(["message" => "职级信息删除成功!"], 204));
    }

    /**
     * 批量恢复职级
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @bodyParam ids array 职级ID,批量恢复+单条恢复,以数组形式
     *
     * @response  204 {"code":204,"msg":"deleted","data":"恢复成功"}
     * @response  400 {"code":400,"msg":"failed","data":"恢复失败/数据格式错误"}
     */
    public function rec(Request $request){
        $ids = $request->input('ids');
        if(is_null($ids) || !is_array($ids)){
            return response()->json(encodeMsg(["message" => "数据格式错误!"], 400));
        }
        try{
            Skill::recover($ids);
            return response()->json(encodeMsg(["message" => "恢复成功!"], 204));
        }catch (\Exception $e){
            return response()->json(encodeMsg(["message" => "恢复失败!"], 400));
        }
    }

    /**
     * 修改员工对应职级
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam skill_id int required 职级id
     * @bodyParam post_id int required 岗位ID
     * @bodyParam ids string required 员工ids:1,2,3
     *
     * @queryParam  skill_id int 职级id
     * @queryParam  post_id int 岗位ID
     * @queryParam  ids string 员工ids:1,2,3
     * @queryParam  remark string 备注
     *
     * @response 200 {"code":200,"msg":"success","data":"添加成功"}
     */
    public function updateInfo(Request $request)
    {
        $skill_id = $request->input('skill_id','');
        $post_id = $request->input('post_id','');
        $ids = $request->input('ids','');
        try{
            foreach (explode(',', $ids) as $key => $value) {
                Employee::where('id',$value)->update(['post_id'=>$post_id,'skill_id'=>$skill_id]);
            }
            return response()->json(encodeMsg(["message" => "添加成功!"], 204));
        } catch (\Exception $e){
            return response()->json(encodeMsg(["message" => "添加失败!"], 400));
        }

    }
}
